import json
import os
import time
from datetime import datetime
import sys

import requests
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.service import Service

import config_setting
from logger import Logger
from lxml import etree

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

from util import format_path

chromedriver_path = "D:/webdriver/chromedriver/chromedriver.exe"
chrome_path = "D:/webdriver/chrome-win64/chrome.exe"
chrome_options = Options()
chrome_options.binary_location = chrome_path
prefs = {
    "download.default_directory": "E:/fitgirl_repack_torrents",
    "download.prompt_for_download": False
}
# chrome_options.add_argument("--headless=new") # for Chrome >= 109
# chrome_options.add_experimental_option("prefs", prefs)
service = Service(executable_path=chromedriver_path)
driver = webdriver.Chrome(service=service, options=chrome_options)

sys.stdout = Logger(datetime.now().strftime("log/torrent_crawler_%Y%m%d_%H%M%S") + ".txt")
batch_name = config_setting.batch_name

base_path = f"E:/fitgirl_repack_data/{batch_name}"
download_base_path = f"E:/fitgirl_repack_torrents/{batch_name}"

# text_headers = {
#     'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
#     'Accept-Encoding': 'gzip, deflate, br, zstd',
#     'Accept-Language': 'zh-CN,zh;q=0.9,zh-TW;q=0.8,en;q=0.7',
#     'Cache-Control': 'max-age=0',
#     'Cookie': '__ddg1_=Op3Fltgxhrl5tXZdYSqS',
#     'Priority': 'u=0, i',
#     'sec-ch-ua': '"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"',
#     'sec-ch-ua-mobile': '?0',
#     'sec-ch-ua-platform': '"Windows"',
#     'sec-fetch-dest': 'document',
#     'sec-fetch-mode': 'navigate',
#     'sec-fetch-site': 'none',
#     'sec-fetch-user': '?1',
#     'upgrade-insecure-requests': '1',
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
# }
text_headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
}
collection = dict()


def get_path(sub_folder, filename, param_data):
    if sub_folder:
        return "%s/%s__%s/%s/%s" % (
            download_base_path, format_path(param_data['serialNo']), format_path(param_data['title']), sub_folder, filename)
    else:
        return "%s/%s__%s/%s" % (
            download_base_path, format_path(param_data['serialNo']), format_path(param_data['title']), filename)


def process_paste_fitgirl_html(src, param_data, download_button_text):
    print(f"requesting html {src}")
    driver.get(src)
    # WebDriverWait(driver, 10).until(lambda d: d.find_element(By.XPATH, "//a[contains(text(), '下载附件')]/@href"))
    time.sleep(8)
    download_link = driver.find_element(By.XPATH, f"//a[contains(text(), '{download_button_text}')]")
    download_link.click()
    # driver.implicitly_wait(5)


def process_sendfile_html(src, param_data):
    print(f"requesting html {src}")
    driver.get(src)
    time.sleep(8)
    download_hint = driver.find_element(By.ID, f"download_click")
    download_hint.click()
    time.sleep(4)
    download_link = driver.find_element(By.ID, f"download_p")
    download_link.click()
    time.sleep(5)

if not os.path.isdir(download_base_path) and os.path.dirname(download_base_path) != "":
    os.makedirs(download_base_path)

for root, dirs, files in os.walk(base_path):
    for file in files:
        # 检查文件扩展名是否为.json
        if file.endswith('.json'):
            # 构造完整的文件路径
            file_path = os.path.join(root, file)
            with open(file_path, 'r', encoding='utf-8') as file:
                # 加载JSON内容
                data = json.load(file)
                data["filepath"] = file_path
                collection[data["serialNo"]] = data
downloadedCount = 1
currentProgress = 0
maxProgress = len(collection.items())
for key, data in collection.items():
    currentProgress += 1
    try:
        print(f"{currentProgress}/{maxProgress} processing {key}: {data['title']}")  # 打印键和值
        torrent_url = data['torrent_urls']
        if "//paste.fitgirl-repacks.site/" in torrent_url:
            downloadedCount += 1
            process_paste_fitgirl_html(torrent_url, data, "下载附件")
            print(f"{downloadedCount} downloaded {torrent_url}")
            pass
        elif "//pastefg.hermietkreeft.site/" in torrent_url:
            downloadedCount += 1
            process_paste_fitgirl_html(torrent_url, data, "Download attachment")
            print(f"{downloadedCount} downloaded {torrent_url}")
            pass
        elif "//sendfile.su/" in torrent_url:
            downloadedCount += 1
            process_sendfile_html(torrent_url, data)
            print(f"{downloadedCount} downloaded {torrent_url}")
            pass
        else:
            print(f"skipped, informer torrent url {torrent_url}")
        # 检查是否已经完成下载过的
        # if os.path.exists(get_path(None, "done.txt", data)):
        #     print(f"skipped {key}: {data['title']}")
        #     continue
        # if os.path.isdir("%s/%s__%s" % (image_base_path, format_path(data['serialNo']), format_path(data['title']))):
        #     continue
    except Exception as ex:
        # self.unknown_error_current_retry += 1
        print(f"error: {data['filepath']}, {ex}")

driver.quit()
print(f"done! total count {downloadedCount}")
